Skip to content

🐛 Add WS pause gating, inbound business queue budget, and extension heartbeat_ack support - #5346

Merged
guiyanakuang merged 1 commit into
mainfrom
fix/ws-pause-gate-queue-budget-ext-heartbeat
Nov 23, 2026
Merged

guiyanakuang merged 1 commit into
mainfrom
fix/ws-pause-gate-queue-budget-ext-heartbeat

Conversation

@guiyanakuang

Copy link
Copy Markdown
Member

Summary

Follow-up to #5345 addressing three WebSocket stability findings surfaced during CrossPaste/crosspaste-mobile#2717 review:

  1. WebSocket pause/resume gating (WsSessionManager, WsClientConnector, WsRouting)

    • Added WsSessionManager.pause() / resume() / isAcceptingSessions() / addOnPausedListener().
    • Calling pause() atomically stops accepting new sessions and handshakes, cancels all in-flight handshakes in WsClientConnector, and closes active sessions via closeAll().
    • registerSession() returns Boolean (rejecting and closing any late-arriving session while paused), and both WsClientConnector and WsRouting respect isAcceptingSessions() and the registerSession() result so mobile background pause cannot race with an in-flight HTTP heartbeat or WS handshake to establish a new session.
  2. Per-session inbound business queue byte budget (WsMessageHandler, WsMessage, WsSession)

    • Added WS_MAX_PENDING_PAYLOAD_BYTES (128 MiB) and WS_MAX_PENDING_MESSAGES (256) to bound queued business payloads in dispatchWsSessionMessages().
    • When a peer pushes faster than the per-session worker drains and exceeds the budget, the overflowing message is not enqueued and the session is closed with CloseReason.Codes.TRY_AGAIN_LATER (1013) so the sender falls back cleanly to its existing retry / HTTP path instead of accumulating unbounded buffers in memory (and without returning WsMessageType.ERROR, which would permanently reject the paste).
  3. Browser extension heartbeat_ack capability and requestId echo (web/src/shared/ws/)

    • Added WsCapability.HEARTBEAT_ACK to ws-types.ts and WS_SUPPORTED_CAPABILITIES in ws-auth.ts.
    • Updated ws-message-handler.ts to echo envelope.requestId in HEARTBEAT_ACK responses (preserving omission for legacy heartbeats without requestId), closing half-open probe false positives for browser extensions.

Verification

  • ./gradlew ktlintFormat && ./gradlew app:desktopTest ktlintCheck — all fast-tier tests (including new pause gating, in-flight handshake cancellation, and inbound queue budget tests) and ktlint passed.
  • cd web && npm test && npx tsc --noEmit — all 15 Vitest files (135 tests) and TypeScript type-checking passed.

@guiyanakuang guiyanakuang left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEEDS FIX

The new overload path drops a clipboard push from the browser extension without a way to retry it. See the inline finding for the sender and receiver paths.

I ran 65 desktop WebSocket unit tests, 5 WebSocket authentication integration tests, all 135 extension tests, the TypeScript type check, and git diff --check. They passed. The current tests cover the receiver's 1013 close and the heartbeat ACK capability and request ID. Browser-to-desktop recovery after an overload has yet to be exercised end to end. The iOS background-to-foreground flow also needs verification after the downstream lifecycle starts calling the new pause and resume methods.

} else {
workChannel.send(envelope)
val payloadBytes = envelope.payload.size.toLong()
if (!tryReserve(payloadBytes)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 — Preserve extension clipboard pushes when the queue overflows

Problem and impact

When the receiver's business queue hits either limit, this branch closes the socket with 1013 and drops the triggering PASTE_PUSH. A clipboard item copied while the desktop receiver is backed up can stay in the extension's history without ever reaching the desktop, even after the socket reconnects.

Evidence

web/src/background/service-worker.ts:247–273 sends the push through wsManager.send() without a requestId. web/src/shared/ws/ws-manager.ts:191–200 reports success after the socket write. Reconnect only reopens the socket, and the extension's HTTP sync pulls desktop history in the other direction (service-worker.ts:412–432). The receiver's overflow test confirms that the triggering envelope is never handled.

Suggestion

Use the existing correlated paste ACK for extension pushes and keep unacknowledged items pending for retry after reconnect. Cover a push dropped by 1013 in a regression test.

Confidence 95/100

@guiyanakuang
guiyanakuang merged commit 9498f76 into main Nov 23, 2026
6 of 7 checks passed
@guiyanakuang
guiyanakuang deleted the fix/ws-pause-gate-queue-budget-ext-heartbeat branch November 23, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant